class Utils {
    /**
     * 设置存储
     * @param {*} keyName
     * @param {*} keyValue
     */
    static setLocalItem(keyName, keyValue) {
      window.localStorage.setItem(keyName, keyValue);
    }
    /**
     * 获取存储
     * @param {*} keyName
     */
    static getLocalItem(keyName) {
      return window.localStorage.getItem(keyName);
    }
  
    /**
     * 清除某个值
     * @param {*} keyName
     */
    static removeLocalItem(keyName) {
      window.localStorage.removeItem(keyName);
    }
  
    /**
     * 保留小数点
     * @param {*} number 数字
     * @param {*} decimal 保留小数位数
     */
    static setFixed(number, decimal) {
      number = Number(number);
      return number.toFixed(decimal);
    }
  
    /**
     * 倒计时
     * @param {*} time
     */
    static doCountDown(time) {
      let timer = {
        expired: false, //是否过期
        day: 0, //剩余天数
        hour: 0, //剩余小时
        minute: 0, //剩余分
        second: 0, //剩余秒
        millsecond: 0 //毫秒
      };
      let countTime = time * 1000;
      let countDate = new Date(countTime).getTime();
      let nowDate = new Date().getTime();
      if (nowDate > countDate) {
        timer.expired = true;
        return timer;
      }
      let diff = countDate - nowDate;
      timer.day = Math.floor(diff / (1000 * 60 * 60 * 24));
      timer.hour = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      timer.minute = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
      timer.second = Math.floor((diff % (1000 * 60)) / 1000);
      timer.millsecond = Math.floor((diff % 1000) / 100);
  
      timer.day = this.setFixed4Time(timer.day);
      timer.hour = this.setFixed4Time(timer.hour);
      timer.minute = this.setFixed4Time(timer.minute);
      timer.second = this.setFixed4Time(timer.second);
      return timer;
    }
  
    /**
     * 时间保留2位数
     * @param {*} number
     */
    static setFixed4Time(number) {
      if (number < 10) {
        return "0" + number;
      }
      return number;
    }
  
    /**
     * 时间格式化
     * @param {*} time
     */
    static timeFormat(time) {
      return new Date(parseFloat(time + "000")).format("yyyy-MM-dd hh:mm:ss");
    }
  
    /**
     * 时间格式化 13位格式化
     * @param {*} time
     */
    static timeFormat13(time) {
      return new Date(parseFloat(time)).format("yyyy-MM-dd hh:mm:ss");
    }
  
    /**
     * 时间格式化
     * @param {*} time
     */
    static timeFormatDay(time) {
      return new Date(parseFloat(time + "000")).format("yyyy-MM-dd");
    }
  
    /**
     * 时间格式化
     * @param {*} time
     */
    static timeFormatMonthDay(time) {
      return new Date(parseFloat(time + "000")).format("MM.dd");
    }
    /**
     * 是否超时
     * @param {*} time
     */
    static timeOver(time) {
      let countTime = time * 1000;
      let countDate = new Date(countTime).getTime();
      let nowDate = new Date().getTime();
      return nowDate > countDate;
    }
  
    /**
     * 手机号格式化
     * @param {*} phone
     */
    static phoneFormat(phone) {
      phone = phone.replace(/[^\d]/g, "");
      let arr = phone.split("");
      let p = "";
      arr.forEach((v, i) => {
        if (i === 3 || i === 7) {
          p += " ";
        }
        p += v;
      });
      return p;
    }
  
    //获取路径参数
    static getQueryStr(name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    }
  
    /**
     * 图片路径转换
     * @param {*} v
     * @param {*} picHost
     */
    static imageFilter(v, picHost) {
      if (
        v !== undefined &&
        v.substr(0, 4) !== "http" &&
        v.indexOf("static/img") == -1
      ) {
        return picHost + v;
      }
      return v;
    }
  
    /**
     * 集合问题
     * @param {*} collection
     * @param {*} id
     */
    static contain(collection, id) {
      for (let i = 0, len = collection.length; i < len; i++) {
        let idi = collection[i];
        if (id == idi) {
          return true;
        }
      }
      return false;
    }
  }
  
  Date.prototype.format = function(fmt) {
    var o = {
      "M+": this.getMonth() + 1, //月份
      "d+": this.getDate(), //日
      "h+": this.getHours(), //小时
      "m+": this.getMinutes(), //分
      "s+": this.getSeconds(), //秒
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      S: this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
        );
    return fmt;
  };
  export default Utils;
  